home *** CD-ROM | disk | FTP | other *** search
- /*
- File: MoreFinderEvents.c
-
- Contains: Functions to help you build and sending Apple events to the Finder.
-
- Written by: Andy Bachorski
-
- Copyright: Copyright © 1996-1999 by Apple Computer, Inc., All Rights Reserved.
-
- You may incorporate this Apple sample source code into your program(s) without
- restriction. This Apple sample source code has been provided "AS IS" and the
- responsibility for its operation is yours. You are not permitted to redistribute
- this Apple sample source code as "Apple sample source code" after having made
- changes. If you're going to re-distribute the source, we require that you make
- it clear in the source that the code was descended from Apple sample source
- code, but that you've made changes.
-
- Change History (most recent first):
- 7/21/1999 Karl Groethe Updated for Metrowerks Codewarror Pro 2.1
-
-
- */
- // A private conditionals file to setup the build environment for this project.
-
- #include "PrivateConditionals.h"
-
-
- //********** Universal Headers ****************************************
-
- #include <Aliases.h>
- #include <ASRegistry.h>
- #include <Folders.h>
- #include <Icons.h>
- #include <OSA.h>
- #include <TextUtils.h>
-
-
- #if UNIVERSAL_INTERFACES_VERSION >= 0x0300
- #include <AEDataModel.h>
- #include <FinderRegistry.h>
- #else
- #include "FinderRegistry.h"
- #endif
-
-
- //********** Project Headers ****************************************
-
- #include "FinderRegistry.h"
- #include "AEHelpers.h"
- #include "ObjectHelpers.h"
- #include "MoreFinderEvents.h"
- #include "ProcessHelpers.h"
-
-
- //******************************************************************************
-
- pascal OSErr MFESetSelectionToNone( const AEIdleUPP idleProcUPP )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = { typeNull, nil }; // If you always init AEDescs, it's always save to dispose of them.
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAESetData, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc containerObj = { typeNull, nil }; // start with the null (application) container
- AEDesc propertyObject = { typeNull, nil };
-
- anErr = OHMakePropertyObject( pSelection, &containerObj, &propertyObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
- AEDisposeDesc( &propertyObject ); // Always dispose of objects as soon as you are done (helps avoid leaks)
- if ( anErr == noErr )
- {
- AEDescList emptyList = { typeNull, nil };
-
- anErr = AECreateList( nil, 0, false, &emptyList );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyAEData, &emptyList );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( idleProcUPP, &theEvent );
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFESetSelectionToNone
-
- //******************************************************************************
-
- pascal OSErr MFEChangeFolderViewNewSuite( const FSSpecPtr fssPtr,
- const long viewStyle,
- const AEIdleUPP idleProcUPP )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAESetData, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc folderObject = {typeNull, nil};
- AEDesc containerObj = { typeNull, nil }; // start with the null (application) container
-
- anErr = OHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &folderObject );
- if ( anErr == noErr )
- {
- AEDesc propertyObject = {typeNull, nil};
-
- anErr = OHMakePropertyObject( pView, &folderObject, &propertyObject );
- AEDisposeDesc( &folderObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
- AEDisposeDesc( &propertyObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamPtr( &theEvent, keyAEData,
- typeLongInteger, &viewStyle, sizeof(viewStyle) );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( idleProcUPP, &theEvent );
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEChangeFolderView
-
- //******************************************************************************
-
- pascal OSErr MFEChangeFolderViewOldSuite( const FSSpecPtr fssPtr,
- const long viewStyle,
- const AEIdleUPP idleProcUPP )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAEFinderEvents, kAEChangeView, &theEvent );
- if ( anErr == noErr )
- {
- anErr = OHAddAliasParameterFromFSS( fssPtr, keyDirectObject, &theEvent );
- if ( anErr == noErr )
- {
- anErr = AEPutParamPtr( &theEvent, keyMiscellaneous,
- typeLongInteger, &viewStyle, sizeof( viewStyle ) );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( idleProcUPP, &theEvent );
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEChangeFolderViewOldSuite
-
- //******************************************************************************
-
- pascal OSErr MFEChangeFolderView( const FSSpecPtr fssPtr,
- const long viewStyle,
- const AEIdleUPP idleProcUPP )
- {
- OSErr anErr = noErr;
-
- if ( FinderCallsAEProcess() )
- {
- if ( FinderIsOSLCompliant() )
- {
- anErr = MFEChangeFolderViewNewSuite( fssPtr, viewStyle, idleProcUPP );
- }
- else
- {
- anErr = MFEChangeFolderViewOldSuite( fssPtr, viewStyle, idleProcUPP );
- }
- }
- else
- {
- anErr = errAEEventNotHandled;
- }
-
- return anErr;
- }//end MFEChangeFolderView
-
- //******************************************************************************
-
- pascal OSErr MFEAddCustomIconToItem( const FSSpecPtr fssPtr,
- const Handle theIconSuite,
- const IconSelectorValue iconSelector,
- const AEIdleUPP idleProcUPP )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAESetData, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc itemObject = {typeNull, nil};
- AEDesc containerObj = { typeNull, nil }; // start with the null (application) container
-
- anErr = OHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &itemObject );
- if ( anErr == noErr )
- {
- AEDesc propertyObject = {typeNull, nil};
-
- anErr = OHMakePropertyObject( pIconBitmap, &itemObject, &propertyObject );
- AEDisposeDesc( &itemObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
- AEDisposeDesc( &propertyObject );
- if ( anErr == noErr )
- {
- AEDescList iconFamilyRec = { typeNull, nil };
-
- anErr = OHMakeIconFamilyRecord( theIconSuite, iconSelector, &iconFamilyRec );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyAEData, &iconFamilyRec );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( idleProcUPP, &theEvent );
- }
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEAddCustomIconToItem
-
- //******************************************************************************
-
- pascal OSErr MFEGetItemIconSuite( const FSSpecPtr fssPtr,
- const AEIdleUPP idleProcUPP,
- Handle *theIconSuite )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAEGetData, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc itemObject = {typeNull, nil};
- AEDesc containerObj = { typeNull, nil }; // start with the null (application) container
-
- anErr = OHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &itemObject );
- if ( anErr == noErr )
- {
- AEDesc propertyObject = {typeNull, nil};
-
- anErr = OHMakePropertyObject( pIconBitmap, &itemObject, &propertyObject );
- AEDisposeDesc( &itemObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
- AEDisposeDesc( &propertyObject );
- if ( anErr == noErr )
- {
- AppleEvent theReply = {typeNull, nil};
- AESendMode sendMode;
-
- if ( idleProcUPP == nil )
- sendMode = kAENoReply;
- else
- sendMode = kAEWaitReply;
-
- anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
- kNoTimeOut, idleProcUPP, nil );
- if ( anErr == noErr )
- {
- anErr = AEHGetHandlerError( &theReply );
- if ( anErr == noErr )
- {
- AEDesc iconFamilyRec = { typeNull, nil };
-
- anErr = AEGetParamDesc( &theReply, keyDirectObject, typeWildCard, &iconFamilyRec );
- if ( anErr == noErr )
- {
- anErr = OHMakeIconSuite( &iconFamilyRec, theIconSuite );
- }
- AEDisposeDesc( &iconFamilyRec );
- }
- AEDisposeDesc( &theReply );
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEGetItemIconSuite
-
- //******************************************************************************
-
- pascal OSErr MFEGetEveryItemOnDesktop( const AEIdleUPP idleProcUPP,
- AEDescList *objectList )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- objectList->descriptorType = typeNull;
- objectList->dataHandle = nil;
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAEGetData, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc containerObj = { typeNull, nil }; // start with the null (application) container
- AEDesc propertyObject = {typeNull, nil};
-
- anErr = OHMakePropertyObject( kDesktopFolderType, &containerObj, &propertyObject );
- if ( anErr == noErr )
- {
- DescType selectAll = kAEAll;
- AEDesc allObjectsDesc = { typeNull, nil };
-
- anErr = OHMakeSelectionObject( selectAll, &propertyObject, &allObjectsDesc );
- AEDisposeDesc( &propertyObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &allObjectsDesc );
- AEDisposeDesc( &allObjectsDesc );
- if ( anErr == noErr )
- {
- AppleEvent theReply = {typeNull, nil};
- AESendMode sendMode;
-
- if ( idleProcUPP == nil )
- sendMode = kAENoReply;
- else
- sendMode = kAEWaitReply;
-
- anErr = AESend( &theEvent, &theReply, sendMode, kAENormalPriority,
- kNoTimeOut, idleProcUPP, nil );
- if ( anErr == noErr )
- {
- anErr = AEHGetHandlerError( &theReply );
-
- if ( !anErr && theReply.descriptorType != typeNull )
- {
- anErr = AEGetParamDesc( &theReply, keyDirectObject, typeAEList, objectList );
- }
- AEDisposeDesc( &theReply );
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEGetEveryItemOnDesktop
-
- //******************************************************************************
-
- pascal OSErr MFEGetViewFontAndSize( const AEIdleUPP idleProcUPP,
- SInt16 *font,
- SInt16 *fontSize )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- if ( idleProcUPP == nil )
- {
- return paramErr;
- }
- // Build the event we will send
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAEGetData, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc containerObj = { typeNull, nil }; // start with the null (application) container
- AEDesc viewPropertyObj = {typeNull, nil};
- // Create an object descriptor for the view preferences property
- anErr = OHMakePropertyObject( pViewPreferences, &containerObj, &viewPropertyObj );
- if ( anErr == noErr )
- {
- AEDesc fontPropertyObj = {typeNull, nil};
-
- // Create an object descriptor for the view font property of the view preferences property
- anErr = OHMakePropertyObject( pViewFont, &viewPropertyObj, &fontPropertyObj );
- // Don't dispose of the fontPropertyObj yet, we will need it for the font size property
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &fontPropertyObj );
- (void) AEDisposeDesc( &fontPropertyObj );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventReturnSInt16( idleProcUPP, &theEvent, font );
- if ( anErr == noErr )
- {
- AEDesc fontSizePropertyObj = {typeNull, nil};
-
- anErr = OHMakePropertyObject( pViewFontSize, &viewPropertyObj, &fontSizePropertyObj );
- (void) AEDisposeDesc( &viewPropertyObj ); // always dispose of AEDescs when you are finished with them
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &fontSizePropertyObj );
- (void) AEDisposeDesc( &fontSizePropertyObj );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventReturnSInt16( idleProcUPP, &theEvent, fontSize );
- }
- }
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEGetEveryItemOnDesktop
-
- //******************************************************************************
-
- pascal OSErr MFEUpdateItemFSS( const FSSpecPtr fssPtr )
- {
- OSErr anErr = noErr;
- AliasHandle aliasHandle;
-
- anErr = NewAlias( nil, fssPtr, &aliasHandle);
-
- if ( anErr == noErr )
- {
- anErr = MFEUpdateItemAlias( aliasHandle );
- }
-
- DisposeHandle( (Handle)aliasHandle );
-
- return anErr;
- } // MFEChangeFolderView
-
- //******************************************************************************
-
- pascal OSErr MFEUpdateItemAlias( const AliasHandle aliasHandle )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAEFinderSuite, kAEUpdate, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc aliasDesc = {typeNull, nil};
-
- anErr = OHMakeAliasDesc( aliasHandle, &aliasDesc );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &aliasDesc );
- AEDisposeDesc( &aliasDesc );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( nil, &theEvent );
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEUpdateItemAlias
-
- //******************************************************************************
-
- pascal OSErr MFESetProcessVisibility( const ProcessSerialNumberPtr psnPtr, Boolean visible )
- {
- OSErr anErr = noErr;
- AppleEvent theEvent = { typeNull, nil };
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAESetData, &theEvent );
- if ( anErr == noErr )
- {
- OSType processType;
- OSType processSignature;
-
- anErr = GetProcessTypeSignature( psnPtr, &processType, &processSignature );
- if ( anErr == noErr )
- {
- AEDesc processObject = { typeNull, nil };
-
- // To hide the Finder it's visible property is directly set to false -- no process target
- // is needed. Only create a non-null processObject if the Finder is not the target.
- if ( (processSignature != kFinderProcessSignature) || (processType != kFinderProcessType) )
- {
- Str32 processName;
-
- // The Finder expects the process to be specified by name.
- anErr = GetProcessName( psnPtr, processName );
- if ( anErr == noErr )
- {
- AEDesc containerObj = { typeNull, nil };
- AEDesc nameDesc = { typeNull, nil };
-
- anErr = AECreateDesc( typeChar, &processName[ 1 ], processName[ 0 ], &nameDesc );
- if ( anErr == noErr )
- {
- anErr = CreateObjSpecifier( cProcess, &containerObj, formName,
- &nameDesc, false, &processObject );
- (void) AEDisposeDesc( &nameDesc );
- }
- }
- }
-
- if ( anErr == noErr )
- {
- AEDesc propertyObject = { typeNull, nil };
-
- // To hide a process tell the Finder to set the process' visible property to false.
- anErr = OHMakePropertyObject( pVisible, &processObject, &propertyObject );
- (void) AEDisposeDesc( &processObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
- (void) AEDisposeDesc( &propertyObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamPtr( &theEvent, keyAEData, typeBoolean, &visible, sizeof(Boolean) );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( nil, &theEvent );
- }
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEChangeFolderView
-
- //******************************************************************************
-
- pascal OSErr MFEOpenFile( const FSSpec *fssPtr )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = { typeNull, nil };
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAEOpen, &theEvent );
- if ( anErr == noErr )
- {
- anErr = AEPutParamPtr( &theEvent, keyDirectObject, typeFSS, fssPtr, sizeof( FSSpec ) );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( nil, &theEvent );
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- }//end MFEOpenFile
-
- //******************************************************************************
-
- pascal OSErr MFEMakeAliasFile( const FSSpecPtr sourceFSSPtr,
- const FSSpecPtr destFSSPtr,
- ConstStr63Param newName,
- const AEIdleUPP idleProcUPP )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = { typeNull, nil };
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAECreateElement, &theEvent );
- if ( anErr == noErr)
- {
- // put the type of item to make into the Apple event
- OSType anAliasType = typeAlias;
-
- anErr = AEPutParamPtr( &theEvent, keyAEObjectClass, typeType, &anAliasType, sizeof(OSType));
- if ( anErr == noErr)
- {
- // add an alias that points to the targer for the alias file to be created
- anErr = OHAddAliasParameterFromFSS( sourceFSSPtr, keyASPrepositionTo, &theEvent );
- if ( anErr == noErr)
- {
- // where should the alias file be created?
- anErr = OHAddAliasParameterFromFSS( destFSSPtr, keyAEInsertHere, &theEvent );
- if ( anErr == noErr)
- {
- // add a properties record so we can specify the name of the new alias file.
- AERecord properties = { typeNull, nil };
-
- anErr = AECreateList( nil, 0, true, &properties );
- if ( anErr == noErr)
- {
- anErr = AEPutKeyPtr( &properties, keyAEName, typeChar, newName+1, *newName );
- if ( anErr == noErr)
- {
- anErr = AEPutParamDesc( &theEvent, keyAEPropData, &properties );
- if ( anErr == noErr)
- {
- // Send the event. In this case we don't care about the reply, but if we did we
- // would get back an object that describes the alias file just created
- // ( either an object descriptor, an alias, or an FSSpec, depending on what we ask for).
- anErr = AEHSendEventNoReturnValue( idleProcUPP, &theEvent );
- }
- }
- AEDisposeDesc( &properties );
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- }//end MFEMakeAliasFile
-
- //******************************************************************************
-
- pascal OSErr MFEMoveDiskIcon( const FSSpecPtr fssPtr,
- const Point position )
- {
- OSErr anErr = noErr;
-
- AppleEvent theEvent = {typeNull, nil};
-
- anErr = AEHMakeEventSignatureTarget( kFinderFileType, kFinderCreatorType,
- kAECoreSuite, kAESetData, &theEvent );
- if ( anErr == noErr )
- {
- AEDesc itemObject = {typeNull, nil};
- AEDesc containerObj = { typeNull, nil }; // start with the null (application) container
-
- anErr = OHMakeAliasObjectFromFSSpec( fssPtr, &containerObj, &itemObject );
- if ( anErr == noErr )
- {
- AEDesc propertyObject = {typeNull, nil};
-
- anErr = OHMakePropertyObject( pPosition, &itemObject, &propertyObject );
- AEDisposeDesc( &itemObject );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyDirectObject, &propertyObject );
- AEDisposeDesc( &propertyObject );
- if ( anErr == noErr )
- {
- AEDescList posList;
-
- anErr = OHMakePositionList( position, &posList );
- if ( anErr == noErr )
- {
- anErr = AEPutParamDesc( &theEvent, keyAEData, &posList );
- if ( anErr == noErr )
- {
- anErr = AEHSendEventNoReturnValue( nil, &theEvent );
- }
- }
- }
- }
- }
- (void) AEDisposeDesc( &theEvent ); // always dispose of AEDescs when you are finished with them
- }
- return anErr;
- } // MFEChangeFolderView
-
- //******************************************************************************
-